{extend name="layout/base"}
{block name='content'}
<body>
<div class="layui-fluid">
    <div class="layui-form">
        <div class="layui-col-md6">
            <div class="layui-form-item">
                <label class="layui-form-label">文章标题：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="title" placeholder="请输入文章标题" lay-verify="required">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择栏目：</label>
                <div class="layui-input-block">
                    <select name="cid" id="" class="layui-select" lay-filter="cate">
                        {foreach $type as $key=>$value }
                            <option value="{$value.cate_id}">{$value.html}{$value.cate_name}</option><br/>
                        {/foreach}
                    </select>
                </div>
            </div>

            <div class="layui-form-item" id="endtime" style="display: none">
                <label class="layui-form-label">活动结束时间：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="endtime" id="test1" >
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">缩略图：</label>
                <div class="layui-input-block">
                    <div id="photo" data-id=""></div>
                    <button type="button" class="layui-btn" id="files" >
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>                    
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">概要：</label>
                <div class="layui-input-block">
                    <textarea name="brief" cols="30" rows="10"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">来源：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="source" placeholder="请输入文章来源">
                </div>
            </div>
        </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容：</label>
                <div class="layui-input-block">
                    <textarea id="Content" cols="30" rows="10"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var Content = UE.getEditor('Content');
    layui.use(['jquery', 'form', 'layer','laydate','colorpicker','upload'], function ($, form, layer,laydate,colorpicker,upload) {
        var uploadInst = upload.render({
            elem: '#files' //绑定元素
            ,url: '{:url(\'Upload/upfile\')}' //上传接口
            ,data: {isThumb: 1} //生成缩略图
            ,done: function(res){
                $('#files').hide();
                var editPath = res.data[0];
                $('#photo').html('<img src="' + res.data + '" alt="" /><span class="close" id="del"></span>');
                $("#photo").attr("data-id",res.data);
                // 上传图片删除
                $('#photo #del').click(function () {
                    $.ajax({
                        type: 'post',
                        url: "{:url('index/delImg')}",
                        data: {path: editPath},
                        success: function (res) {
                            if (res.code == 0) {
                                $('#files').show();
                                layer.msg(res.msg)
                                $('#photo').empty()
                                parent.location.reload();
                            } else {
                                layer.msg(res.msg)
                            }
                        }
                    })
                })
            }
            ,error: function(){
                layer.msg('上传异常', {icon: 5});
            }
        });
        laydate.render({
            elem: '#test1'
        });

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            ,color: '#1c97f5'
            ,done: function(color){
                $('#test-form-input').val(color);
            }
        });

        form.on('select(cate)', function(data){
            if (data.value == 5) {
                $('#endtime').show()
            } else {
                $('#endtime').hide()
            }
        });        
        form.on('submit(submit)', function (obj) {
            delete obj.field.file;
            var content = Content.getContent();
            if (content == '') {
                layer.msg('内容不能为空');
                return false;
            }            
            var imgPath =  $("#photo").data("id");
            if(imgPath){
                obj.field.image = imgPath;
            }
            if(content){
                obj.field.content = content;
            }
            $.ajax({
                type: 'post',
                url: "{:url('article/add')}",
                data: obj.field,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 6});
                        location.href="{:url('article/lists')}";
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function () {
                    layer.msg('请求错误', {icon: 5});
                }
            })
        });
    });
</script>
{/block}